package com.lucene.blog.controller;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lucene.blog.bean.Blog;
import com.lucene.blog.bean.Blogger;
import com.lucene.blog.bean.Comment;
import com.lucene.blog.db.service.BlogService;
import com.lucene.blog.db.service.BloggerService;
import com.lucene.blog.db.service.CommentService;
import com.lucene.blog.lucene.BlogIndex;
import com.lucene.blog.util.PageUtil;
import com.lucene.blog.util.StringUtil;

/**
 * 博客Controller层
 * @author yachao
 *
 */
@Controller
@RequestMapping("/blog")
public class BlogController {

	@Resource
	private BloggerService bloggerService;
	@Resource
	private BlogService blogService;
	@Resource
	private CommentService commentService;
	
	private BlogIndex blogIndex = new BlogIndex();
	
	/**
	 * 请求博客详细信息
	 * @param blogId
	 * @param request
	 * @return
	 */
	@RequestMapping("/articles/{blogId}")
	public ModelAndView details(@PathVariable("blogId") Integer blogId, HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		
		try {
			Blogger blogger = bloggerService.getBloggerData();
			Blog blog = blogService.findBlogById(blogId);
			
			String keywords = blog.getKeyword();
			if(StringUtil.isNotEmpty(keywords)) {
				String[] strKeyword = keywords.split(" ");
				List<String> keywordList = StringUtil.filterSpace(Arrays.asList(strKeyword));
				modelAndView.addObject("keywords", keywordList);
			} else {
				modelAndView.addObject("keywords", null);
			}
			
			modelAndView.addObject("blogger", blogger);
			modelAndView.addObject("blog", blog);
			blog.setClickHit(blog.getClickHit() + 1); // 将访问量加一
			blogService.updateBlog(blog);
			
			// 查询评论信息
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("blogId", blogId);
			map.put("state", 1);
			List<Comment> commentList = commentService.getCommentData(map);
			
			modelAndView.addObject("commentList", commentList);
			modelAndView.addObject("commonPage", "front/blog/blogDetail.jsp");
			modelAndView.addObject("title", blog.getTitle() + " - 张亚超的技术博客园");
			
			// 存入上一篇和下一篇的显示代码
			modelAndView.addObject("pageCode", PageUtil.getPrevAndNextPageCode(blogService.getPrevBlog(blogId), blogService.getNextBlog(blogId), request.getServletContext().getContextPath()));
			modelAndView.setViewName("mainTemp");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return modelAndView;
	}
	
	/**
	 * 根据关键字查询博客信息
	 * @param q
	 * @param page
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/search")
	public ModelAndView search(@RequestParam(value = "q", required = false) String q,
							   @RequestParam(value = "page", required = false) String page,
							   HttpServletRequest request) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		int pageSize = 10;
		List<Blog> blogIndexList = blogIndex.searchBlog(q);
		
		if(page == null) { // page为null表示第一次搜索
			page = "1";
		}
		int fromIndex = (Integer.parseInt(page) - 1) * pageSize;
		int toIndex = blogIndexList.size() >= Integer.parseInt(page)*pageSize ? Integer.parseInt(page)*pageSize : blogIndexList.size();
		
		modelAndView.addObject("blogIndexList", blogIndexList.subList(fromIndex, toIndex));
		modelAndView.addObject("pageCode", PageUtil.getUpAndDownPageCode(Integer.parseInt(page), blogIndexList.size(), q, pageSize, request.getServletContext().getContextPath()));
		modelAndView.addObject("q", q);
		modelAndView.addObject("resultTotal", blogIndexList.size());
		modelAndView.addObject("commonPage", "/front/blog/searchResult.jsp");
		modelAndView.addObject("title", "搜索'" + q + "'的结果 - 张亚超的技术博客园");
		modelAndView.setViewName("mainTemp");
		return modelAndView;
	}
}
